<?php
//createAssignment.php
//provides the form an instructor can use to create an assignment - rjs
$allow=true;
require_once("overhead.php");
if ($currentRole != "instructor") {
	throwError(2);
}
$crumb["link"]="index.php?cid=".$currentCourse."&uid=".$currentUser."&role=".$currentRole;
$crumb["title"]="Course Home";
$breadcrumbs[]=$crumb;
$pageTitle="Create Assignment";
$error="";

if (isset($_POST['assignmentTitle']) && isset($_POST['assignmentDescription'])) {

	$createAssignmentQuery=mysql_query("insert into assignment (Status, Name, Description,CourseID) values ('Closed','".mysql_real_escape_string($_POST['assignmentTitle'])."','".mysql_real_escape_string($_POST['assignmentDescription'])."',(select ID from course where Token='".$currentCourse."'))");
	if ($createAssignmentQuery) {
		$assignmentid = mysql_insert_id();
		if (mkdir("assignments/assignment_".$assignmentid, 0770)) {
			if (!mkdir("assignments/assignment_".$assignmentid."/submissions",0770)) { 
				$error="<font color='red'>Failed. Could not create submissions directory.</font><br />";
			}
			elseif (!mkdir("assignments/assignment_".$assignmentid."/copy",0770)) {
				$error="<font color='red'>Failed. Could not create tests directory.</font><br />";
			}
			else {
				if ($_FILES["junitFile"]["error"] > 0) {
  					echo "Error: " . $_FILES["junitFile"]["error"] . "<br />";
  				} else {
  					if (!move_uploaded_file($_FILES["junitFile"]["tmp_name"],"assignments/assignment_".$assignmentid."/instructorTests/t.zip")) {
  						$msg="Couldn't upload file.";
					} else {
						$msg="Successfully created assignment.";
					}
 				}
				header("location:viewAssignment.php?msg=".$msg."&aid=".$assignmentid);
				exit;
			}
		} else { $error="<font color='red'>Failed. Could not create assignment directory.</font><br />"; }
	} else { $error="<font color='red'>Failed.</font><br />"; }
}
?>
<html>
	<head>
		<title>Create an Assignment</title>
	</head>
	<body>
		<?php printBreadcrumbs(); ?>
		<h2>Create an assignment</h2>
		<?php print $error; ?>
		<form method='post' enctype='multipart/form-data'>
			Assignment name:<br /><input type='text' name='assignmentTitle' /><br />	
			Upload a JUnit Test:<br />
			<input type="file" name="junitFile" /><br />
			Assignment Description:<br />
			<textarea name='assignmentDescription' style='width:100%;' rows='15'></textarea>
			<input type='hidden' name='cid' value='<?php print $currentCourse; ?>' />
			<input type='hidden' name='uid' value='<?php print $currentUser; ?>' />
			<input type='hidden' name='role' value='<?php print $currentRole; ?>' />
			<input type='submit' value='Create Assignment'>
		</form>
	</body>
</html>